package uiUtil;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import errorLayer.*;
import modelLayer.TeachingBlock;
import controlLayer.ConflictCtrl;
import controlLayer.ScheduleCtrl;
import uiLayer.TeachingBlockGUI;

public class TeachingBlockDialog extends JDialog {
	
	private final JPanel contentPane = new JPanel();
	private ScheduleCtrl sc;
	private ConflictCtrl cc = new ConflictCtrl();
	private TeachingBlockGUI tbGUI;
	private TeachingBlock tb;
	private JButton addToSchedule;
	private JButton cancelBtn;
	private int selectedID;
	private String classNameConflict;
	private String selectedStaff;
	private String selectedSubject;
	private String selectedRoom;
	
	public static void main(String[] args) {
		
		try {
			
			TeachingBlockDialog dialog = new TeachingBlockDialog(null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public TeachingBlockDialog (ScheduleCtrl scheduleCtrl) {
		
		this.sc = scheduleCtrl;
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Teaching Block");
		setSize(750, 235);
		setLocationRelativeTo(null);
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(contentPane, BorderLayout.CENTER);
		
		contentPane.setSize(500, 200);
		contentPane.setLayout(new BorderLayout(0,0));
		
		tbGUI = new TeachingBlockGUI();
		tbGUI.setSize(500, 200);
		contentPane.add(tbGUI, BorderLayout.CENTER);
		
		addToSchedule = new JButton("Add to schedule");
		addToSchedule.setPreferredSize(new Dimension(150, 25));
		addToSchedule.setMinimumSize(new Dimension(150, 25));
		addToSchedule.setMaximumSize(new Dimension(150, 25));
		
		addToSchedule.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				
				cc.importSchedules(sc.getSelectedBlockNo());
				boolean roomConflict = cc.checkRoomConflict(sc.getSelectedBlockNo(), tbGUI.getSelection().getRoom());
				boolean staffConflict = cc.checkStaffConflict(sc.getSelectedBlockNo(), tbGUI.getSelection().getStaff());
				
				if (!roomConflict && !staffConflict) {
					
					setTb(tbGUI.getSelection());
					dispose();
					
				}
				else {
					if (staffConflict) {
						new staffAlreadyWorking(tbGUI.getSelection().getStaff().getName(), cc.getConflictStaffClasses().toString());
						System.out.println("That staff member is already assigned during that time.");
					}
					
					if (roomConflict) {
						new roomAlreadyInUse(tbGUI.getSelection().getRoom().getName(), cc.getConflictRoomClasses().toString());
						System.out.println("That room is already in use during that time.");
					}
				}
				
			}
		});
		
		cancelBtn = new JButton("Cancel");
		cancelBtn.setPreferredSize(new Dimension(150, 25));
		cancelBtn.setMinimumSize(new Dimension(150, 25));
		cancelBtn.setMaximumSize(new Dimension(150, 25));
		
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				
				dispose();
				
			}
		});
		
		tbGUI.dialogFix(addToSchedule, cancelBtn);
		
	}

	public int getSelectedID() {
		return selectedID;
	}

	public void setSelectedID(int selectedID) {
		this.selectedID = selectedID;
	}
	
	public void setTb (TeachingBlock teachingBlock) {
		
		tb = teachingBlock;
		
	}
	
	public TeachingBlock getTB () {
		return tb;
	}

}
